<%@ page language="java" import="java.util.*,org.edu.bean.Province" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <script src="/scripts/jquery.js"></script>
    <link href="/styles/common.css" rel="stylesheet" type="text/css"/>
    <title></title>
    <style>
    	.out{
    		padding:20px 0 0 200px;
    	}
    	.out div{
    		height:40px;
    	}
    	.out div font{
    		display:block ;
    		width:80px;
    		float:left;
    	}
    	.out div select{
    		float:left;
    		margin:0 20px 0 0;
    	}
    </style>
	

  </head>
  
  <body>
  	<div class="out">
    	<form action="/userMan/adduser.action" method="POST">
    		<input type="hidden" value="<c:out value="${userId}" />" id="id"  />
    		<div><font>名称</font><input name="user.userName" type="text" id="username" /></div>
    		<div><font>编号</font><input name="user.systemNumber" readonly="readonly" id="sysnumber" /></div>
    		<div>
	    		<font>是否启用</font>
	    		<select name="user.status" id="status">
	    			<option value="1" selected="selected">是</option>
	    			<option value="0">否</option>
	    		</select>
	    	</div>
    		<div id="address">
	    		<font>省</font>
	    		<select name="user.workProvinceId" id="province">
	    			<option value="0">--省--</option>
	    			<c:forEach var="province" items="${provinces}" >
						<option value="<c:out value="${province.proid}" />"><c:out value="${province.proname}" /></option>
					</c:forEach>
	    		</select>
	    		<font>市</font>
	    		<select name="user.workCityId" id="city">
	    			<option value="0">--市--</option>
	    		</select>
	    		<font>区/县</font>
	    		<select name="user.workCountyId" id="county">
	    			<option value="-1">--区/县--</option>
	    		</select>
	    	</div>
    		<div id="but">
    			<input type="submit" value="下一步" id="subUser"/>
    			<input type="reset" value="重置"/>
    		</div>
    	</form>
    </div>
  </body>
</html>
<script type="text/javascript">
var oldPro = $("#province").val();
var oldCity = $("#city").val();
var oldCounty = $("#county").val();
$(document).ready(function(){
	if($("#id").val() >0){
			var userId = $("#id").val();
			$("#username").attr("readonly","readonly");
			$("#but").remove();
			ajaxFindUser(userId);
			$("#status").change(function(){
				var status_num = $("#status").val();
				ajaxChengStatus(userId,status_num);
			});
			$("#province").change(function(){
				var proId = $("#province").val();
				ajaxChangePro(userId,proId);
			});
			$("#city").change(function(){
				var cityId = $("#city").val();
				ajaxChangeCity(userId,cityId);
			});
			$("#county").change(function(){
				var countyId = $("#county").val();
				ajaxChangeCounty(userId,countyId);
			});
	}
	$("#province").change(function(){
		clearCityData();
		clearCountyData();
		var prId= $("#province").val();
		changeCity(prId);

	});
	$("#city").change(function(){
		clearCountyData();
		var ciId = $("#city").val();
		changeCounty(ciId);
	});
});
function ajaxChengStatus(userId,status){
	$.ajax({
		type:"POST",
		url:"/userajax/updateUser_status.action",
		data:{"userId":userId,"user_status":status},
		dataType:"json",
		success:function(msg){
			var data = eval("("+msg+")");
			if(data.status){
			}else{
				alert("操作失败");
				if(status ==1){
					$("#status").val(0);
				}else{
					$("#status").val(1);
				}
			}
		},
	});
}
function ajaxChangePro(userId,proId){
	$.ajax({
		type:"POST",
		url:"/userajax/updateUser_pro.action",
		data:{"userId":userId,"proId":proId},
		dataType:"json",
		success:function(msg){
			var data = eval("("+msg+")");
			if(data.status){
				changeCity(proId);
				$("#city").val(0);
				clearCountyData();
				$("#county").val(0);
				oldPro = proId;
			}else{
				alert("修改操作失败");
				$("#province").val(oldPro);
			}
		},
	});
}
function ajaxChangeCity(userId,cityId){
	$.ajax({
		type:"POST",
		url:"/userajax/updateUser_city.action",
		data:{"userId":userId,"cityId":cityId},
		dataType:"json",
		success:function(msg){
			var data = eval("("+msg+")");
			if(data.status){
				changeCounty(cityId);
				$("#county").val(0);
				oldCity = cityId ;
			}else{
				alert("修改操作失败");
				$("#city").val(oldcity);
			}
		},
	});
}
function ajaxChangeCounty(userId,countyId){
	$.ajax({
		async:false,
		type:"POST",
		url:"/userajax/updateUser_county.action",
		data:{"userId":userId,"countyId":countyId},
		dataType:"json",
		success:function(msg){
			var data= eval("("+msg+")");
			if(data.status){
				oldCounty = countyId ;
			}else{
				alert("修改操作失败");
				$("#county").val(oldCounty);
			}
		}
	});
}
function ajaxFindUser(userId){
	$.ajax({
		async:false,
		type:"POST",
		url:"/ajaxUser/useredit.action",
		data:{"userId":userId},
		dataType:"json",
		success:function(msg){
			var data = eval("("+msg+")");
			if(data.status){
				$("#username").val(data.userName);
				$("#sysnumber").val(data.systemNumber);
				$("#status").val(data.userstatus);
				$("#province").val(data.workProvinceId);
				if(data.workProvinceId != -1){
					changeCity(data.workProvinceId);
					$("#city").val(data.workCityId);
					if(data.workCityId != -1){
						changeCounty(data.workCityId);
						$("#county").val(data.workCountyId);
					}
				}
			}
		},
	});
}
function clearCityData(){
	$("#city").html("<option value='0'>--市--</option>");
}

function clearCountyData(){
	$("#county").html("<option value='-1'>--区/县--</option>");
}
function changeCity(prId){
	$.ajax({
		async:false,
		type:"POST",
		data:{"prId":prId,},
		url:"/xzqh/city.action",
		dataType:"json",
		success:function(msg){
			var data = eval("("+msg+")");
			var htmlStr = "<option value='0'>--市--</option>" ;
			for(var i = 0 ; i<data.length ; i++){
				htmlStr += "<option value='"+data[i]['cityid']+"'>"+data[i]['cityname']+"</option>";
			}
			$("#city").html(htmlStr);
		},
	});
}
function changeCounty(ciId){
	$.ajax({
		async:false,
		type:"POST",
		data:{"ciId":ciId},
		url:"/xzqh/county.action",
		dataType:'json',
		success:function(msg){
			var data = eval("("+msg+")");
			var htmlStr = "<option value='-1'>--县/区--</option>" ;
			for(var i = 0 ; i<data.length ; i++){
				htmlStr += "<option value='"+data[i]['countyid']+"'>"+data[i]['countyname']+"</option>";
			}
			$("#county").html(htmlStr);
		},
	});
}
</script>
